/*  -*- mode: c++; coding: utf-8; c-file-style: "stroustrup"; -*-

    Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
    This file is part of Intromit.

    Intromit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Intromit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Intromit.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "introsight.hh"
#include "ogl.hh"
#include "event_mng.hh"
#include "render_mng.hh"

#include <sys/time.h>

// DEFINITIONS
#define IS_WINDOWS_TITLE      "Introsight"

CLASS_INIT(introsight); // INTROSIGHT CLASS

introsight::introsight()
{
    CLASS_CTOR(introsight);
    ctor();
}

introsight::~introsight()
{
    CLASS_DTOR(introsight);
    dtor();
}

void introsight::ctor()
{
    w=NULL;
    em=NULL;
    rm=NULL;
}

void introsight::dtor()
{
    if(w) w=(ogl_window*)w->decref();
    if(em) em=(event_mng*)em->decref();
    if(rm) rm=(render_mng*)rm->decref();
}

bool introsight::init()
{
    if(!w)
    {
	w=new ogl_window;
	w->init(true/*managed*/, const_cast<char*>(IS_WINDOWS_TITLE));
    }
    if(!w->set_events(OGL_ALLX_EVENT)) ERROR("cannot set event mask");
    if(!em) em=new event_mng;
    if(!em->init(w)) return false;

    // set context and initialize render manager
    if(!rm) rm=new render_mng;
    if(!rm->init(w, em)) return false;
    return true;
}

int introsight::refresh()
{
    return rm->render();
}
